﻿Imports System.Runtime.InteropServices

Imports NHunspell.Hunspell
Public Class frmSpellcheck
    Dim spelltext As String() = Nothing
    Dim correct As Boolean = True
    Dim suggestions As List(Of String) 'list of suggestion for misspelled word
    Dim hunspell As New NHunspell.Hunspell("Dictionaries/dhivehi.aff", "Dictionaries/dhivehi.dic") 'spell checking engine
    Public IgnoreList As New List(Of String)
    Dim ErrorWordTem As String
    Dim myfont1 As New Font("MV Boli", 10)
    Dim countchecked As Integer = 0
    Dim ignoreOnce As List(Of String) = Nothing

    Private Sub frmSpellcheck_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rtbInput.Text = frmMain.rtbInput.Text
        Clear()
        CheckSpelling(rtbInput.Text)
    End Sub


    Private Sub CheckSpelling(ByVal text As String)
        lstSuggestion.Items.Clear()
        spelltext = text.Replace(vbCrLf, " ").Split(CChar(" "))
        'checking all the words
        If countchecked >= spelltext.Count - 1 Then
            If MessageBox.Show("އިމްލާކުށްތައް ބަލާ ރަގަނޅުކޮށް ނިމިއްޖެ", "ނިމުނީ", MessageBoxButtons.OK, MessageBoxIcon.Information) = Windows.Forms.DialogResult.OK Then
                frmMain.rtbInput.Text = Me.rtbInput.Text
                Me.Close()
            End If
            Exit Sub
        End If
        For i As Integer = countchecked To spelltext.Length - 1
            countchecked += 1
            correct = hunspell.Spell(spelltext(i))
            If correct = False AndAlso IgnoreList.Contains(spelltext(i)) = False Then
                ErrorWordTem = spelltext(i)
                Dim suggestions As List(Of String)
                suggestions = hunspell.Suggest(spelltext(i))
                For Each word In suggestions
                    lstSuggestion.Items.Add(word)
                Next
                Dim pos As Integer = 0
                Dim searchtext As String = spelltext(i)
                pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
                Do While pos <> -1
                    Dim myfont As New Font("MV Boli", 11, FontStyle.Bold)
                    If rtbInput.SelectedText = searchtext Then
                        rtbInput.SelectionLength = searchtext.Length
                        rtbInput.SelectionColor = Color.Red
                    End If
                    pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
                    Exit For
                Loop
            End If
        Next

    End Sub

    Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChange.Click
        Dim pos As Integer = 0
        Dim searchtext As String = ErrorWordTem
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
                rtbInput.SelectedText = lstSuggestion.Text
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
            Exit Do
        Loop
        CheckSpelling(rtbInput.Text)
    End Sub

    Private Sub btnChangeAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeAll.Click
        Dim pos As Integer = 0
        Dim searchtext As String = ErrorWordTem
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
                rtbInput.SelectedText = lstSuggestion.Text
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
        Loop
        CheckSpelling(rtbInput.Text)
    End Sub



    Private Sub btnIgnore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIgnore.Click
        Dim pos As Integer = 0
        Dim searchtext As String = ErrorWordTem
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
                'rtbInput.SelectedText = searchtext
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
            Exit Do
        Loop
        CheckSpelling(rtbInput.Text)
    End Sub

    Private Sub btnIgnoreAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIgnoreAll.Click
        Dim pos As Integer = 0
        Dim searchtext As String = ErrorWordTem
        IgnoreList.Add(ErrorWordTem)
        pos = rtbInput.Find(searchtext, pos, RichTextBoxFinds.MatchCase)
        Do While pos <> -1
            If rtbInput.SelectedText = searchtext Then
                rtbInput.SelectionLength = searchtext.Length
                rtbInput.SelectionColor = Color.Black
                rtbInput.SelectionFont = myfont1
                'rtbInput.SelectedText = lstSuggestion.Text
            End If
            pos = rtbInput.Find(searchtext, pos + 1, RichTextBoxFinds.MatchCase)
        Loop
        CheckSpelling(rtbInput.Text)
    End Sub

    Private Sub frmSpellcheck_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Leave
        frmMain.rtbInput.Text = Me.rtbInput.Text
    End Sub

    Private Sub Clear()
        Me.rtbInput.SelectAll()
        Me.rtbInput.SelectionFont = myfont1
        Me.rtbInput.SelectionColor = Color.Black
        Me.rtbInput.DeselectAll()
        Me.rtbInput.Select(0, 0)
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        frmMain.rtbInput.Text = Me.rtbInput.Text
        Me.Close()
    End Sub


#Region "FORM MOVE"
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub frmSpellcheck_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub frmSpellcheck_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub frmSpellcheck_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub
    
#End Region

    
    
End Class